<template>
  <div class="container">
    <div class="top">
      <el-input
        style="width: 200px; margin-bottom: 5px"
        v-model="search.serial_code"
        placeholder="请输入改账单号"
        clearable
      ></el-input>

      <el-input
        style="width: 200px; margin-bottom: 5px"
        v-model="search.st_serial_code"
        placeholder="请输入结算单号"
        clearable
      ></el-input>
      <el-select v-model="search.status" placeholder="请选择审核状态">
        <el-option
          v-for="item in businessType"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-input
        style="width: 300px"
        :disabled="true"
        placeholder="请搜索供应商信息"
        v-model="supplierNameList.name"
      >
        <template slot="prepend"
          >原供应商</template
        >
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="supplierBtn"
        ></el-button>
      </el-input>
      <el-input
        style="width: 300px"
        :disabled="true"
        placeholder="请搜索供应商信息"
        v-model="newSupplierNameList.name"
      >
        <template slot="prepend"
          >新供应商</template
        >
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="newSupplierBtn"
        ></el-button>
      </el-input>

      <el-input
        style="width: 300px"
        :disabled="true"
        placeholder="请搜索商品信息"
        v-model="commodityNameList.name"
      >
        <template slot="prepend"
          >商品</template
        >
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="commodityBtn"
        ></el-button>
      </el-input>
      <el-date-picker
        value-format="yyyy-MM-dd"
        v-model="date"
        type="daterange"
        :clearable="false"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
      ></el-date-picker>
      <el-button
        type="primary"
        class="handleSearch"
        icon="el-icon-search"
        @click="handleSearch()"
        >查询</el-button
      >
      <el-button
        type="danger"
        class="handleSearch"
        icon="el-icon-delete"
        @click="empty()"
        >清空</el-button
      >
      <el-button type="info" icon="el-icon-tickets" @click="derive()"
        >导出</el-button
      >

      <el-button icon="el-icon-circle-plus" type="primary" @click="addData"
        >添加改账单</el-button
      >
      <el-button icon="el-icon-circle-plus" type="primary" @click="addDatas"
        >批量添加改账单</el-button
      >
      <el-button
        type="warning"
        icon="el-icon-edit-outline"
        @click="bulkConfirms()"
        >审核</el-button
      >
      <el-button
        type="warning"
        icon="el-icon-edit-outline"
        @click="bulkCompulsoryConfirms()"
        >强审</el-button
      >
      <el-button
        type="danger"
        icon="el-icon-delete"
        @click="handleDeletes(scope)"
        >删除</el-button
      >
    </div>
    <el-table
      max-height="600"
      :data="tableData"
      ref="multipleTable"
      tooltip-effect="dark"
      border
      style="width: 100%"
      empty-text="暂无数据"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        align="center"
        :selectable="selectable"
      ></el-table-column>
      <el-table-column
        prop="serial_code"
        label="改账单号"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="st_serial_code"
        label="原结算单"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="st_status_desc"
        label="财务结算状态"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="stsu_code"
        label="原供应商编码"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="stsu_name"
        label="原供应商名称"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="goods_code"
        label="商品编码"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="goods_name"
        label="商品名称"
        align="center"
      ></el-table-column>
      <!-- <el-table-column
        prop="unit"
        label="单位"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="spec"
        label="规格"
        align="center"
      ></el-table-column> -->
      <el-table-column
        prop="goods_num"
        label="原数量"
        align="center"
      ></el-table-column>

      <el-table-column
        prop="goods_price"
        label="原单价"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="goods_amount"
        label="原金额"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="charge_code"
        label="原供应商冲单结算单"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="new_code"
        label="新供应商结算单"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="supplier_code"
        label="新供应商编码"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="supplier_name"
        label="新供应商名称"
        align="center"
      ></el-table-column>

      <el-table-column
        prop="num"
        label="新数量"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="price"
        label="新单价"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="amount"
        label="新金额"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="buyer_code"
        label="采购员编码"
        align="center"
      ></el-table-column>

      <el-table-column
        prop="buyer"
        label="采购员名称"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="class_code"
        label="品类编码"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="class_name"
        label="品类名称"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="rdc_code"
        label="配送中心编码"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="rdc_name"
        label="配送中心名称"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="examine_time"
        label="审核时间"
        align="center"
      ></el-table-column>

      <el-table-column label="操作" width="300" align="center">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-edit"
            v-if="scope.row.status != 1"
            @click="handleEdit(scope)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="warning"
            icon="el-icon-edit-outline"
            v-if="scope.row.status != 1"
            @click="confirms(scope)"
            >审核</el-button
          >
          <el-button
            size="mini"
            type="warning"
            icon="el-icon-edit-outline"
            v-if="scope.row.status != 1"
            @click="compulsoryConfirms(scope)"
            >强审</el-button
          >

          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            v-if="scope.row.status != 1"
            @click="handleDelete(scope)"
            >删除</el-button
          >
          <el-button
            v-if="scope.row.status == 1"
            type="info"
            size="mini"
            icon="el-icon-info"
            @click="particulars(scope)"
            >详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination
      @page="handleSizeChange"
      @pagesize="handleCurrentChange"
      :pageData="pageData"
    ></pagination>
    <!-- 添加、修改Dailog -->
    <addChangeAccountDialog
      :dialog="dialog"
      @upData="getData"
    ></addChangeAccountDialog>
    <!-- 批量添加、批量修改Dailog -->
    <addChangeAccountBatchDialog
      :dialog="batchDialog"
      @upData="getData"
    ></addChangeAccountBatchDialog>
    <changeAccountParticulars
      :dialog="particularsDialog"
    ></changeAccountParticulars>
    <commodityDialog
      :commoditydialog="commoditydialog"
      @tag="commodityTag"
    ></commodityDialog>

    <supplierSearch
      :supplierdialog="newSupplierdialog"
      @tag="newSupplierTag"
    ></supplierSearch>
    <supplierSearch
      :supplierdialog="supplierdialog"
      @tag="supplierTag"
    ></supplierSearch>
  </div>
</template>
<script>
import { getToken } from "@/utils/auth";
import Pagination from "@/components/pagination";
import AddChangeAccountDialog from "@/components/changeAccount/addChangeAccountDialog";
import AddChangeAccountBatchDialog from "@/components/changeAccount/addChangeAccountBatchDialog";
import SupplierSearch from "@/components/grossMargin/supplierSearch";
import CommodityDialog from "@/components/grossMargin/commodityDialog";
import ChangeAccountParticulars from "@/components/changeAccount/changeAccountParticulars";
import {
  correctList,
  examineCorrect,
  forceExamineCorrect,
  delCorrect,
  examineCorrectBatch,
  forceExamineCorrectBatch
} from "@/api/changeAccount/changeAccount";
export default {
  components: {
    pagination: Pagination,
    commodityDialog: CommodityDialog,
    addChangeAccountDialog: AddChangeAccountDialog,
    changeAccountParticulars: ChangeAccountParticulars,
    addChangeAccountBatchDialog: AddChangeAccountBatchDialog,
    supplierSearch: SupplierSearch
  },
  data() {
    return {
      //页面数据
      tableData: [],
      businessType: [
        {
          value: "0",
          label: "全部"
        },
        {
          value: "1",
          label: "未审核"
        },
        {
          value: "2",
          label: "已审核"
        }
      ],
      multipleSelection: [],
      //分页
      pageData: {
        pageSizes: [10, 20],
        pageSize: 10,
        page: 1,
        total: null,
        layout: "total, sizes, prev, pager, next, jumper"
      },
      supplierNameList: {
        name: "",
        code: ""
      },
      newSupplierNameList: {
        name: "",
        code: ""
      },
      commodityNameList: {
        name: "",
        code: ""
      },
      commoditydialog: {
        centerDialogVisible: false
      },
      particularsDialog: {
        dialogVisible: false,
        id: ""
      },
      supplierdialog: {
        centerDialogVisible: false
      },
      newSupplierdialog: {
        centerDialogVisible: false
      },
      search: {
        status: "",
        serial_code: "",
        st_serial_code: "",
        start_time: "",
        end_time: ""
      },

      //dialog信息
      dialog: {
        dialogVisible: false,
        id: ""
      },
      batchDialog: {
        dialogVisible: false,
        id: ""
      },
      date: []
    };
  },

  created() {
    //页面数据
    this.getData();
  },
  methods: {
    selectable(row, index) {
      if (row.status == "1") {
        return false;
      } else {
        return true;
      }
    },
    //页面数据
    getData() {
      let data = {
        limit: this.pageData.pageSize,
        page: this.pageData.page,
        serial_code: this.search.serial_code,
        st_serial_code: this.search.st_serial_code,
        start_time: this.search.start_time,
        end_time: this.search.end_time,
        goods_code: this.commodityNameList.code,
        status: this.search.status,
        old_supplier_id: this.supplierNameList.code,
        new_supplier_id: this.newSupplierNameList.code
      };
      correctList(data)
        .then(res => {
          if (res.code == 0) {
            this.tableData = res.data.correct.data;
            this.pageData.page = res.data.correct.current_page;
            this.pageData.pagesize = res.data.correct.per_page;
            this.pageData.total = res.data.correct.total;
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    //超级供应商搜索
    supplierBtn() {
      this.supplierdialog = {
        centerDialogVisible: true
      };
    },
    newSupplierBtn() {
      this.newSupplierdialog = {
        centerDialogVisible: true
      };
    },
    supplierTag(data) {
      this.supplierNameList = {
        name: "",
        code: ""
      };
      let arrName = [];
      let arrCode = [];
      data.forEach(res => {
        arrName.push(res.name);
        arrCode.push(res.id);
      });
      this.supplierNameList.name = arrName.join(",");
      this.supplierNameList.code = arrCode.join(",");
    },
    newSupplierTag(data) {
      this.newSupplierNameList = {
        name: "",
        code: ""
      };
      let arrName = [];
      let arrCode = [];
      data.forEach(res => {
        arrName.push(res.name);
        arrCode.push(res.id);
      });
      this.newSupplierNameList.name = arrName.join(",");
      this.newSupplierNameList.code = arrCode.join(",");
    },
    //超级商品搜索
    commodityBtn() {
      this.commoditydialog = {
        centerDialogVisible: true
      };
    },
    commodityTag(data) {
      this.commodityNameList = {
        name: "",
        code: ""
      };
      let arrName = [];
      let arrCode = [];
      data.forEach(res => {
        arrName.push(res.name);
        arrCode.push(res.code);
      });
      this.commodityNameList.name = arrName.join(",");
      this.commodityNameList.code = arrCode.join(",");
    },
    //删除
    handleDelete(scope) {
      let data = {
        id: scope.row.id
      };
      this.$confirm("是否删除该条信息?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          delCorrect(data).then(res => {
            if (res.code == 0) {
              this.getData();
              this.$message({
                type: "success",
                message: res.msg || res.message
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消"
          });
        });
    },
    //批量删除
    handleDeletes(scope) {
      if (this.multipleSelection.length > 0) {
        let ids = [];
        this.multipleSelection.forEach(res => {
          ids.push(res.id);
        });
        let data = {
          id: ids.join(",")
        };
        this.$confirm("是否删除该条信息?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            delCorrect(data).then(res => {
              if (res.code == 0) {
                this.getData();
                this.$message({
                  type: "success",
                  message: res.msg || res.message
                });
              }
            });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消"
            });
          });
      } else {
        this.$message({
          type: "info",
          message: "请勾选内容"
        });
      }
    },
    //审核
    confirms(scope) {
      let data = {
        id: scope.row.id
      };
      this.$confirm("是否审核信息?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          examineCorrect(data).then(res => {
            if (res.code == 0) {
              this.getData();
              this.$message({
                type: "success",
                message: res.msg
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消"
          });
        });
    },
    compulsoryConfirms(scope) {
      let data = {
        id: scope.row.id
      };
      this.$confirm("是否审核信息?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          forceExamineCorrect(data).then(res => {
            if (res.code == 0) {
              this.getData();
              this.$message({
                type: "success",
                message: res.msg
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消"
          });
        });
    },
    // 批量审核
    bulkConfirms() {
      if (this.multipleSelection.length > 0) {
        let ids = [];
        this.multipleSelection.forEach(res => {
          ids.push(res.id);
        });
        let data = {
          old_supplier_id: this.supplierNameList.code,
          new_supplier_id: this.newSupplierNameList.code,
          ids: ids.join(",")
        };
        console.log(data, 9999);
        this.$confirm("是否审核?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            examineCorrectBatch(data).then(res => {
              if (res.code == 0) {
                this.getData();
                this.$message({
                  type: "success",
                  message: res.msg
                });
              }
            });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消"
            });
          });
      } else {
        this.$message({
          type: "info",
          message: "请勾选内容"
        });
      }
    },
    // 批量墙审
    bulkCompulsoryConfirms() {
      if (this.multipleSelection.length > 0) {
        let ids = [];
        this.multipleSelection.forEach(res => {
          ids.push(res.id);
        });
        let data = {
          old_supplier_id: this.supplierNameList.code,
          new_supplier_id: this.newSupplierNameList.code,
          ids: ids.join(",")
        };
        this.$confirm("是否强审?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            forceExamineCorrectBatch(data).then(res => {
              if (res.code == 0) {
                this.getData();
                this.$message({
                  type: "success",
                  message: res.msg
                });
              }
            });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消"
            });
          });
      } else {
        this.$message({
          type: "info",
          message: "请勾选内容"
        });
      }
    },
    //导出
    derive() {
      this.$confirm("是否导出?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let link = document.createElement("a");
          if (this.date.length > 0) {
            this.search.start_time = this.date[0];
            this.search.end_time = this.date[1];
          }
          link.href =
            this.$axios.defaults.baseURL +
            "/api/correct/exportCorrectDetail?token=" +
            getToken() +
            "&serial_code=" +
            this.search.serial_code +
            "&st_serial_code=" +
            this.search.st_serial_code +
            "&goods_code=" +
            this.commodityNameList.code +
            "&start_time=" +
            this.search.start_time +
            "&end_time=" +
            this.search.end_time +
            "&status=" +
            this.search.status +
            "&old_supplier_id=" +
            this.supplierNameList.code +
            "&new_supplier_id=" +
            this.newSupplierNameList.code;
          link.click();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消导出"
          });
        });
    },
    // 本地搜索
    handleSearch() {
      if (this.date.length > 0) {
        this.search.start_time = this.date[0];
        this.search.end_time = this.date[1];
      }
      this.pageData.page = 1;
      this.pageData.total = null;
      this.getData();
    },
    //添加
    addData() {
      this.dialog.dialogVisible = true;
      this.dialog.dialogType = "";
      this.dialog.id = "";
    },
    //批量添加改账单
    addDatas() {
      this.batchDialog.dialogVisible = true;
      this.batchDialog.dialogType = "";
      this.batchDialog.id = "";
    },
    //编辑
    handleEdit(scope) {
      this.dialog.dialogVisible = true;
      this.dialog.dialogType = "edit";
      this.dialog.id = scope.row.id;
    },
    // 详情
    particulars(scope) {
      this.particularsDialog.dialogVisible = true;
      this.particularsDialog.id = scope.row.id;
    },

    //清空
    empty() {
      this.date = [];
      this.search = {
        status: "",
        serial_code: "",
        st_serial_code: "",
        start_time: "",
        end_time: ""
      };
      this.commodityNameList.name = "";
      this.commodityNameList.code = "";
      this.supplierNameList.name = "";
      this.supplierNameList.code = "";
      this.newSupplierNameList.name = "";
      this.newSupplierNameList.code = "";
    },

    //每页多少条
    handleCurrentChange(val) {
      this.pageData.page = 1;
      this.pageData.pageSize = val;
      this.getData();
    },
    //选取页数
    handleSizeChange(page) {
      this.pageData.page = page;
      this.getData();
    },
    //选取
    handleSelectionChange(val) {
      this.multipleSelection = val;
    }
  }
};
</script>
<style scoped>
.top {
  position: relative;
  margin-bottom: 5px;
}
.btnList {
  position: relative;
  margin-bottom: 5px;
}
.add {
  position: absolute;
  right: 0;
  top: 4px;
}
.pagination {
  text-align: right;
  padding: 10px;
}
</style>
